using HKB.Common;
using System;
using System.Data;
using System.Drawing;
using System.Web.UI.WebControls;
namespace HKB.Web
{
	public class UpLoadFile
	{
		private string _errString;
		public FileUpload myfile = null;
		public string Suffix = "";
		public string SaveInFoder = "";
		public string NewFileName = "";
		private FileIO m_file = new FileIO();
		public string ErrString
		{
			get
			{
				return this._errString;
			}
		}
		public string UploadPic(FileUpload FileUpload1, DataRow config, string oldfile)
		{
			string text = "";
			Random random = new Random();
			string result;
			if (FileUpload1.HasFile)
			{
				bool flag = false;
				this.Suffix = config["UploadType"].ToString();
				string[] array = this.Suffix.Split(new char[]
				{
					','
				});
				string fileName = FileUpload1.FileName;
				string text2 = fileName.Substring(fileName.LastIndexOf('.')).ToLower();
				for (int i = 0; i < array.Length; i++)
				{
					if (text2 == array[i].Trim().ToLower())
					{
						flag = true;
					}
				}
				if (!flag)
				{
					this._errString = Language.Get("Shop/Product/UpLoadTypeFail", this.Suffix);
					result = string.Empty;
				}
				else
				{
					if (FileUpload1.PostedFile.ContentLength < Convert.ToInt32(config["UploadPicmaxSize"]) * 1024)
					{
						string str;
						if (this.NewFileName != "")
						{
							str = this.NewFileName;
						}
						else
						{
							str = DateTime.Now.ToString("yyyyMMddHHmm") + random.Next(1000, 9999).ToString();
						}
						text = DateTime.Now.ToString("yyyyMM") + "/" + str + text2;
						string str2 = (this.SaveInFoder == "") ? (config["InstallDir"].ToString() + "UploadFile/ShopPic/") : this.SaveInFoder;
						string truePath = this.m_file.GetTruePath(str2 + text);
						if (!this.m_file.UploadFile(FileUpload1.PostedFile, truePath))
						{
							this._errString = Language.Get("Shop/Product/UpLoadPicFail");
							result = string.Empty;
						}
						else
						{
							if (!string.IsNullOrEmpty(oldfile))
							{
								this.m_file.DeleteFile(this.m_file.GetTruePath(oldfile));
							}
							string text3 = truePath;
							int num = Text.ToInt32(config["Wartermark_Type"]);
							if (num != 0)
							{
								Watermark watermark = new Watermark();
								float num2 = Convert.ToSingle(config["Watermark_PhotoAlpha"]);
								float hScale = 0.01f * Convert.ToSingle(config["Watermark_PositionX"]);
								switch (num)
								{
								case 1:
								{
									string textFont = config["Wartermark_Text"].ToString();
									float num3 = 0.01f * Convert.ToSingle(config["Watermark_PositionY"]);
									string sFontFamily = config["Wartermark_FontFamily"].ToString();
									bool bFontBold = config["Wartermark_FontWeight"].ToString() == "1";
									watermark.zzsTextWater(text3, textFont, text3, hScale, num3, Convert.ToInt32(num2), sFontFamily, bFontBold);
									break;
								}
								case 2:
								{
									string truePath2 = this.m_file.GetTruePath(config["Watermark_PhotoUrl"].ToString());
									float num3 = 100f * Convert.ToSingle(config["Watermark_PositionX"]);
									int intDistance = Convert.ToInt32(config["Watermark_Position"]);
									watermark.zzsImgWater(text3, truePath2, text3, num2, num3, intDistance);
									break;
								}
								}
							}
							text = str2 + text;
							Watermark watermark2 = new Watermark();
							int num4 = Convert.ToInt32(config["SamllPic_DefaultWidth"]);
							int num5 = Convert.ToInt32(config["SamllPic_DefaultHieght"]);
							watermark2.CreateSmallPic(Fetch.GetTruePath(text), Fetch.GetTruePath(text.Replace(".", string.Concat(new object[]
							{
								"_",
								num4,
								"_",
								num5,
								"."
							}))), num4, num5);
							result = text;
						}
					}
					else
					{
						this._errString = Language.Get("Shop/Product/UpLoadTypeLarget", config["UploadPicmaxSize"].ToString());
						result = string.Empty;
					}
				}
			}
			else
			{
				result = text;
			}
			return result;
		}
		public string UploadVideo(FileUpload FileUpload1, DataRow config, string oldfile)
		{
			string result;
			if (FileUpload1.HasFile)
			{
				bool flag = false;
				string text = config["UploadVideoType"].ToString();
				string[] array = text.Split(new char[]
				{
					','
				});
				string fileName = FileUpload1.FileName;
				string text2 = fileName.Substring(fileName.LastIndexOf('.')).ToLower();
				for (int i = 0; i < array.Length; i++)
				{
					if (text2 == array[i].Trim().ToLower())
					{
						flag = true;
					}
				}
				if (!flag)
				{
					this._errString = Language.Get("Shop/Product/UpLoadTypeFail", text);
					result = string.Empty;
				}
				else
				{
					if (FileUpload1.PostedFile.ContentLength < Convert.ToInt32(config["UploadVidmaxSize"]) * 1024)
					{
						string str;
						if (this.NewFileName != string.Empty)
						{
							str = this.NewFileName;
						}
						else
						{
							str = DateTime.Now.ToString("yyyyMMddmmss");
						}
						string text3 = DateTime.Now.ToString("yyyyMM") + "/" + str + text2;
						string str2 = (this.SaveInFoder == "") ? (config["InstallDir"].ToString() + "UploadFile/ShopVideo/") : this.SaveInFoder;
						string truePath = this.m_file.GetTruePath(str2 + text3);
						if (!this.m_file.UploadFile(FileUpload1.PostedFile, truePath))
						{
							this._errString = Language.Get("Shop/Product/UpLoadPicFail");
							result = string.Empty;
						}
						else
						{
							if (!string.IsNullOrEmpty(oldfile))
							{
								this.DelVideo(oldfile);
							}
							text3 = str2 + text3;
							result = text3;
						}
					}
					else
					{
						this._errString = Language.Get("Shop/Product/UpLoadTypeLarget", config["UploadVidmaxSize"].ToString());
						result = string.Empty;
					}
				}
			}
			else
			{
				result = string.Empty;
			}
			return result;
		}
		public void DelVideo(string VideoPath)
		{
			if (!string.IsNullOrEmpty(VideoPath) && !VideoPath.ToLower().Contains("http"))
			{
				this.m_file.DeleteFile(this.m_file.GetTruePath(VideoPath));
			}
		}
		public bool UpLoad(string path)
		{
			string truePath = this.m_file.GetTruePath(path);
			bool result;
			try
			{
				this.m_file.UploadFile(this.myfile.PostedFile, truePath);
			}
			catch (Exception ex)
			{
				this._errString = ex.Message.ToString();
				result = false;
				return result;
			}
			result = true;
			return result;
		}
		public string UploadTryProduct(FileUpload FileUpload1, DataRow config, string oldfile)
		{
			string text = "";
			Random random = new Random();
			string result;
			if (FileUpload1.HasFile)
			{
				bool flag = false;
				this.Suffix = config["UploadType"].ToString();
				string[] array = this.Suffix.Split(new char[]
				{
					','
				});
				string fileName = FileUpload1.FileName;
				string text2 = fileName.Substring(fileName.LastIndexOf('.')).ToLower();
				for (int i = 0; i < array.Length; i++)
				{
					if (text2 == array[i].Trim().ToLower())
					{
						flag = true;
					}
				}
				if (!flag)
				{
					this._errString = Language.Get("Shop/Product/UpLoadTypeFail", this.Suffix);
					result = string.Empty;
				}
				else
				{
					if (FileUpload1.PostedFile.ContentLength < Convert.ToInt32(config["UploadPicmaxSize"]) * 1024)
					{
						string str;
						if (this.NewFileName != "")
						{
							str = this.NewFileName;
						}
						else
						{
							str = DateTime.Now.ToString("yyyyMMddHHmm") + random.Next(1000, 9999).ToString();
						}
						text = DateTime.Now.ToString("yyyyMM") + "/" + str + text2;
						string str2 = (this.SaveInFoder == "") ? (config["InstallDir"].ToString() + "UploadFile/TryProduct/") : this.SaveInFoder;
						string truePath = this.m_file.GetTruePath(str2 + text);
						if (!this.m_file.UploadFile(FileUpload1.PostedFile, truePath))
						{
							this._errString = Language.Get("Shop/Product/UpLoadPicFail");
							result = string.Empty;
						}
						else
						{
							if (!string.IsNullOrEmpty(oldfile))
							{
								this.m_file.DeleteFile(this.m_file.GetTruePath(oldfile));
							}
							string text3 = truePath;
							int num = Text.ToInt32(config["Wartermark_Type"]);
							if (num != 0)
							{
								Watermark watermark = new Watermark();
								float num2 = Convert.ToSingle(config["Watermark_PhotoAlpha"]);
								float hScale = 0.01f * Convert.ToSingle(config["Watermark_PositionX"]);
								switch (num)
								{
								case 1:
								{
									string textFont = config["Wartermark_Text"].ToString();
									float num3 = 0.01f * Convert.ToSingle(config["Watermark_PositionY"]);
									string sFontFamily = config["Wartermark_FontFamily"].ToString();
									bool bFontBold = config["Wartermark_FontWeight"].ToString() == "1";
									watermark.zzsTextWater(text3, textFont, text3, hScale, num3, Convert.ToInt32(num2), sFontFamily, bFontBold);
									break;
								}
								case 2:
								{
									string truePath2 = this.m_file.GetTruePath(config["Watermark_PhotoUrl"].ToString());
									float num3 = 100f * Convert.ToSingle(config["Watermark_PositionX"]);
									int intDistance = Convert.ToInt32(config["Watermark_Position"]);
									watermark.zzsImgWater(text3, truePath2, text3, num2, num3, intDistance);
									break;
								}
								}
							}
							text = str2 + text;
							Watermark watermark2 = new Watermark();
							int num4 = Convert.ToInt32(config["SamllPic_DefaultWidth"]);
							int num5 = Convert.ToInt32(config["SamllPic_DefaultHieght"]);
							watermark2.CreateSmallPic(Fetch.GetTruePath(text), Fetch.GetTruePath(text.Replace(".", string.Concat(new object[]
							{
								"_",
								num4,
								"_",
								num5,
								"."
							}))), num4, num5);
							result = text;
						}
					}
					else
					{
						this._errString = Language.Get("Shop/Product/UpLoadTypeLarget", config["UploadPicmaxSize"].ToString());
						result = string.Empty;
					}
				}
			}
			else
			{
				result = text;
			}
			return result;
		}
		public string UploadShopPrc(FileUpload FileUpload1, string FolderPath, int ShopID, int Type)
		{
			string result;
			if (FileUpload1.HasFile && FileUpload1.FileName != "")
			{
				string fileName = FileUpload1.PostedFile.FileName;
				string text = fileName.Substring(fileName.LastIndexOf("\\") + 1);
				string text2 = fileName.Substring(fileName.LastIndexOf(".") + 1);
				if (text2.ToUpper() == "JPG" || text2.ToUpper() == "GIF" || text2.ToUpper() == "PNG")
				{
					System.Drawing.Image image = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
					if (Type == 1)
					{
						if (image.Width > 200 && image.Height > 70)
						{
							image.Dispose();
							result = "1";
						}
						else
						{
							result = "./UpLoadFile/ShopLogo/" + ShopID.ToString() + "." + text2;
							string path = "/Store/UpLoadFile/ShopLogo/" + ShopID.ToString() + "." + text2;
							string truePath = this.m_file.GetTruePath(path);
							FileUpload1.SaveAs(truePath);
						}
					}
					else
					{
						if (image.Width > 950 && image.Height > 150)
						{
							image.Dispose();
							result = "1";
						}
						else
						{
							result = "./UpLoadFile/ShopBackground/" + ShopID.ToString() + "." + text2;
							string path = "/Store/UpLoadFile/ShopBackground/" + ShopID.ToString() + "." + text2;
							string truePath = this.m_file.GetTruePath(path);
							FileUpload1.SaveAs(truePath);
						}
					}
				}
				else
				{
					result = "2";
				}
			}
			else
			{
				result = "3";
			}
			return result;
		}
	}
}
